//
//  TFRecorder.m
//  Recorder
//
//  Created by chenTengfei on 15/1/30.
//  Copyright (c) 2015年 chenTengfei. All rights reserved.
//

#import "TFRecorder.h"

#define kRecordAudioFile @"myRecord.caf"

@interface TFRecorder ()



@end

@implementation TFRecorder

#pragma mark - 公共方法
- (BOOL)record
{
    if (![self isRecording]) {
        [self tf_setAudioSession];
        
//        _savePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//        _savePath = [self.savePath stringByAppendingPathComponent:kRecordAudioFile];
//        NSLog(@"------record file path: %@", self.savePath);
        
        return [super record];
    }
    return NO;
}

- (TFRecorder *)recordWithSavePath:(NSString *)savePath
{
    if (![self isRecording]) {
        [self tf_setAudioSession];
        
        _savePath = savePath;
        
        //创建录音文件保存路径
        NSURL *url=[NSURL fileURLWithPath:self.savePath];
        //创建录音格式设置
        NSDictionary *setting=[self tf_getAudioSetting];
        //创建录音机
        NSError *error=nil;
        TFRecorder *audioRecorder = [[TFRecorder alloc]initWithURL:url settings:setting error:&error];
        
        audioRecorder.meteringEnabled = self.meteringEnabled;//YES;//如果要监控声波则必须设置为YES
        if (error) {
            NSLog(@"创建录音机对象时发生错误，错误信息：%@",error.localizedDescription);
            return nil;
        }
        
        [audioRecorder record];    //首次使用应用时如果调用record方法会询问用户是否允许使用麦克风
        return audioRecorder;
    }
    return self;
}

- (instancetype)init
{
    NSString *defaultSaveStr = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    defaultSaveStr = [defaultSaveStr stringByAppendingPathComponent:kRecordAudioFile];
    
    //创建录音文件保存路径
    NSURL *url=[NSURL fileURLWithPath:defaultSaveStr];
    //创建录音格式设置
    NSDictionary *setting=[self tf_getAudioSetting];
    //创建录音机
    NSError *error=nil;
    
    self = [self initWithURL:url settings:setting error:&error];
    if (error) {
        NSLog(@"创建录音机对象时发生错误，错误信息：%@",error.localizedDescription);
        return nil;
    } else {
        _savePath = defaultSaveStr;
        return self;
    }
}

#pragma mark - 私有方法

/**
 *  设置音频会话
 */
- (void)tf_setAudioSession {
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
}

/**
 *  取得录音文件设置
 *
 *  @return 录音设置
 */
-(NSDictionary *)tf_getAudioSetting{
    NSMutableDictionary *dicM=[NSMutableDictionary dictionary];
    //设置录音格式
    [dicM setObject:@(kAudioFormatLinearPCM) forKey:AVFormatIDKey];
    //采样率
    [dicM setValue :[NSNumber numberWithFloat:11025.0] forKey: AVSampleRateKey];//44100.0
    //通道数
    [dicM setValue :[NSNumber numberWithInt:2] forKey: AVNumberOfChannelsKey];
    //线性采样位数
    //[recordSettings setValue :[NSNumber numberWithInt:16] forKey: AVLinearPCMBitDepthKey];
    //音频质量,采样质量
    [dicM setValue:[NSNumber numberWithInt:AVAudioQualityHigh] forKey:AVEncoderAudioQualityKey];
    return dicM;
}
@end
